<template>
  <div class="app-container">
    <el-form ref="form" :model="hospitalSet" label-width="100px">
      
      <el-form-item label="医院名称">
        <el-input v-model="hospitalSet.hosname"></el-input>
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospitalSet.hoscode"></el-input>
      </el-form-item>
      <el-form-item label="url">
        <el-input v-model="hospitalSet.apiUrl"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名">
        <el-input v-model="hospitalSet.contactsName"></el-input>
      </el-form-item>
      <el-form-item label="联系人手机">
        <el-input v-model="hospitalSet.contactsPhone"></el-input>
      </el-form-item>
      
  
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>  
  </div>
</template>

<script>
import { add, getHospitalSet, update } from '@/api/hospset'

export default {
  data() {
    return {
      hospitalSet:{}
    }
  },
  created() {
    if(this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.getData(id)
    }else{
      this.hospitalSet={}
    }
  },
  methods: {
    getData(id) {
      getHospitalSet(id)
        .then(response => {
          console.log(response) 
          this.hospitalSet=response.data
        }).catch(error => {
          console.log(error)
        })
    },
    onSubmit() {
      this.$confirm('操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if(!this.hospitalSet.id) {
          this.toUpdate()
        }else{
          this.toAdd()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '操作失败'
        })
      })
    },
    toAdd() {
      update(this.hospitalSet)
        .then(response => {
          console.log(response)
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          this.hospitalSet={}
          this.$router.push({path:'/hospSet/list'})
        }).catch(error => {
          console.log(error)
        })
    },
    toUpdate() {
      add(this.hospitalSet)
        .then(response => {
          console.log(response)
          this.$message({
            type: 'success',
            message: '修改成功!'
          })
          this.hospitalSet={}
          this.$router.push({path:'/hospSet/list'})
        }).catch(error => {
          console.log(error)
        })
    }
  }
}
</script>
